﻿using System;
using PowerToolsWeb.DataAccess;
using PowerToolsWeb.Facade;


namespace PowerToolsWeb.Admin
{
    public partial class WebMessageManage : AdminPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindMessage();
            }
        }

        private void BindMessage()
        {
            Tpt_CompanyinfoCollection tcc = new Tpt_CompanyinfoCollection();
            tcc.ListAll();
            if (tcc.Count != 0)
            {
                Tpt_Companyinfo tc = tcc[0];
                this.txtAddress.Text = tc.ADDRESS;
                this.txtCompanyName.Text = tc.COMPANYNAME;
                this.txtContactor.Text = tc.CONTACTOR;
                this.txtContactTel.Text = tc.CONTACTTEL;
                this.txtIntroduction.Text = tc.INTRODUCTION;
                this.txtMobilePhone.Text = tc.MOBILEPHONE;
                this.txtQQ.Text = tc.QQ;
                this.txtQQSigkey.Text = tc.QQSIGKEY;
            }
        }

        private bool Check()
        {
            if (this.txtCompanyName.Text.Trim() == string.Empty)
            {
                Alert(this.Page, "请填写公司名称!");
                return false;
            }
            if (this.txtContactor.Text.Trim() == string.Empty)
            {
                Alert(this.Page, "请填写联系人!");
                return false;
            }
            if (this.txtContactTel.Text.Trim() == string.Empty && this.txtMobilePhone.Text.Trim() == string.Empty)
            {
                Alert(this.Page, "联系人电话和手机号码至少填写一个!");
                return false;
            }
            //if (this.txtQQ.Text.Trim() == string.Empty)
            //{
            //    Alert(this.Page, "请填写QQ号码!");
            //    return false;
            //}
            //if (this.txtQQSigkey.Text.Trim() == string.Empty)
            //{
            //    Alert(this.Page, "请填写QQSigkey!");
            //    return false;
            //}
            if (this.txtAddress.Text.Trim() == string.Empty)
            {
                Alert(this.Page, "请填写地址!");
                return false;
            }
            if (this.txtIntroduction.Text.Trim() == string.Empty)
            {
                Alert(this.Page, "请填写公司简介!");
                return false;
            }
            if (this.txtIntroduction.Text.Trim().Length > 4000)
            {
                Alert(this.Page, "公司简介太长!");
                return false;
            }
            return true;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!this.Check())
                return;
            Tpt_CompanyinfoCollection tcc = new Tpt_CompanyinfoCollection();
            tcc.ListAll();
            if (tcc.Count == 0)
            {
                Tpt_Companyinfo tc = new Tpt_Companyinfo();
                tc.COMPANYNAME = this.txtCompanyName.Text.Trim();
                tc.CONTACTOR = this.txtContactor.Text.Trim();
                tc.MOBILEPHONE = this.txtMobilePhone.Text.Trim();
                tc.CONTACTTEL = this.txtContactTel.Text.Trim();
                tc.QQ = this.txtQQ.Text.Trim();
                tc.QQSIGKEY = this.txtQQSigkey.Text.Trim();
                tc.ADDRESS = this.txtAddress.Text.Trim();
                tc.INTRODUCTION = this.txtIntroduction.Text.Trim();
                if (tc.Insert())
                {
                    Alert(this.Page, "添加成功!");
                }
                else
                {
                    Alert(this.Page, "添加失败!");
                }
            }
            else
            {
                Tpt_Companyinfo tc = tcc[0];
                tc.COMPANYNAME = this.txtCompanyName.Text.Trim();
                tc.CONTACTOR = this.txtContactor.Text.Trim();
                tc.MOBILEPHONE = this.txtMobilePhone.Text.Trim();
                tc.CONTACTTEL = this.txtContactTel.Text.Trim();
                tc.QQ = this.txtQQ.Text.Trim();
                tc.QQSIGKEY = this.txtQQSigkey.Text.Trim();
                tc.ADDRESS = this.txtAddress.Text.Trim();
                tc.INTRODUCTION = this.txtIntroduction.Text.Trim();
                if (tc.Update())
                {
                    Alert(this.Page, "修改成功!");
                }
                else
                {
                    Alert(this.Page, "修改失败!");
                }
            }
        }
    }
}
